package com.ruoyi.project.mall.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.PageUtils;
import com.ruoyi.common.utils.bootdo.Query;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.domain.OrderReviewDO;
import com.ruoyi.project.mall.service.OrderReviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 订单审核表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-01-20 10:17:42
 */
 
@Controller
@RequestMapping("/mall/orderReview")
public class OrderReviewController {
	@Autowired
	private OrderReviewService orderReviewService;
	
	@GetMapping()
	@RequiresPermissions("mall:orderReview:orderReview")
	String OrderReview(){
	    return "mall/orderReview/orderReview";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("mall:orderReview:orderReview")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&& StringUtils.isNotBlank(params.get("name").toString())){
            List<OrderReviewDO> orderReviewList = orderReviewService.likeList(query);
            int total = orderReviewService.likeListCount(query);
            PageUtils pageUtils = new PageUtils(orderReviewList, total);
            return pageUtils;
		}

		List<OrderReviewDO> orderReviewList = orderReviewService.list(query);
		int total = orderReviewService.count(query);
		PageUtils pageUtils = new PageUtils(orderReviewList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("mall:orderReview:add")
	String add(){
	    return "mall/orderReview/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("mall:orderReview:edit")
	String edit(@PathVariable("id") String id,Model model){
		OrderReviewDO orderReview = orderReviewService.get(id);
		model.addAttribute("orderReview", orderReview);
	    return "mall/orderReview/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:orderReview:add")
	public R save(OrderReviewDO orderReview){
	    orderReview.setId(UuidUtil.get32UUID());
	    orderReview.setDelFlag(0);
	    orderReview.setCaAt(new Date());
		if(orderReviewService.save(orderReview)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("mall:orderReview:edit")
	public R update( OrderReviewDO orderReview){
		orderReview.setUpAt(new Date());
		orderReviewService.update(orderReview);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:orderReview:remove")
	public R remove( String id){
		OrderReviewDO orderReview = orderReviewService.get(id);
		orderReview.setDelFlag(1);
		orderReview.setUpAt(new Date());
		if(orderReviewService.update(orderReview)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:orderReview:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				OrderReviewDO orderReview = orderReviewService.get(string);
				orderReview.setDelFlag(1);
				orderReview.setUpAt(new Date());
				orderReviewService.update(orderReview);
			}
		}
	
		return R.ok();
	}
	
}
